package org.liaohailong.helloworld;

import org.junit.Test;
import org.liaohailong.helloworld.find.BinarySearch;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: liaohailong
 * Time: 2021/11/8 10:48
 * Describe:
 */
public class FindExam implements AUtil.ILog {

    @Test
    public void binarySearch() {
//        int[] args = {0, 1, 2, 3,  5, 6, 7, 8};
        int[] args = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        List<Integer> list = new ArrayList<>();
        for (int arg : args) list.add(arg);
        int fromIndex = 0;
        int toIndex = list.size();
        int target = 4;
        int index = BinarySearch.find(list, fromIndex, toIndex, target);
        log("binarySearch index = " + index);
        if (index < 0) {
            int insert = -index;
            log("binarySearch list size = " + list.size() + ", insert index  = " + insert);
            list.add(insert, target);
            log(list.toString());
        }
    }

    public void log(String msg) {
        log("FindExam", msg);
    }

    @Override
    public void log(String tag, String msg) {
        System.out.println(tag + ": " + msg);
    }
}
